package com.javasm.demobike;

import java.util.Arrays;
import java.util.Date;

public class ShareBikeCompany implements ShareBikeDao{

    private int  id;

    private String name;

    private ShareBike[] shareBikes;

    private int sum;
//单车被借出的次数
    private int count;

    private  int bikeIndex = 1;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ShareBike[] getShareBikes() {
        return shareBikes;
    }

    public void setShareBikes(ShareBike[] shareBikes) {
        this.shareBikes = shareBikes;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public ShareBikeCompany(int id, String name, ShareBike[] shareBikes, int sum, int count) {
        this.id = id;
        this.name = name;
        this.shareBikes = shareBikes;
        this.sum = sum;
        this.count = count;
    }

    public ShareBikeCompany() {
    }

    @Override
    public void addBike(int num) {
        if(sum + num > shareBikes.length){
            shareBikes = Arrays.copyOf(shareBikes,(shareBikes.length + num )*2);
            System.out.println("扩容后的容量是"+shareBikes.length);
        }
        for (int i = 0; i < num; i++) {
            shareBikes[sum]= new ShareBike(bikeIndex++,1,null);
            sum++;
        }
        System.out.println("投放成功了+++++");

    }

    @Override
    public void removeBike(int biekId) {
        for (int i = 0; i < sum; i++) {
                ShareBike shareBike = shareBikes[i];
           if(shareBike.getId() == biekId){
               if(shareBike.getStatus() == 1){
                   for (int j = 0; j < sum-1; j++) {
                       shareBikes[ j ] = shareBikes [j + 1];
                   }
                   sum --;
                   shareBikes[sum] = null;
               }else {
                   System.out.println("这个车已经被借，不能删除！！！！！");
               }
                return;
           }
        }

        System.out.println("真的没找到对应的数值，请重新输入");

    }

    @Override
    public void queryBike() {

    }

    @Override
    public void borrowBike(int bikeId) {
        for (int i = 0; i < sum; i++) {
            ShareBike shareBike = shareBikes[i];
            if(shareBike.getId() == bikeId){
                if (shareBike.getStatus() == 1){
                    shareBike.setStatus(0);
                    shareBike.setBorrowTime(new Date());
                    System.out.println("借出成功了");
                    count++;
                }else {
                    System.out.println("此车已经被借出，不能重复借出！！！");
                }
                return;
            }
        }
        System.out.println("额没有你要找的ID，你仔细看看你要找的东西是不是写错了！请重新选择！");
    }

    @Override
    public void backBike(int bikeId) {
        for (int i = 0; i < sum; i++) {
            if(shareBikes[i].getId() == bikeId){
                if(shareBikes[i].getStatus() == 0){
                    Date backdate = new Date();
                    Date borrowTime = shareBikes[i].getBorrowTime();
                    long shiyongshijian =-(borrowTime.getTime()- backdate.getTime())/1000;
                    System.out.println("您一共使用了" + shiyongshijian + "秒的车辆");
                    System.out.println("您需要付费是" + shiyongshijian * 0.1 + "元");

                    shareBikes[i].setStatus(1);
                    shareBikes[i].setBorrowTime(null);
                }
                else{
                    System.out.println("此车尚未借出，无需归还！");
                }
                return;
            }
        }
        System.out.println("没有对应的车辆的ID,请重新输入你的ID");
    }
}
